<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>可可文章管理系统</title>
<link href="../template/css/ke.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://img.kekenet.com/statics/js/jquery.min.js"></script>
</head>
<body>


<!-- main -->
<div class="issCont">

  <div class="SelectTab">
    <h1 class="SelectTab_title">选择栏目：</h1>
   	<select name="catid" size="12">
	  <option value="0" selected="selected">不限栏目</option>
	  {{foreach from=$sub_cats item=c1}}
	    <option data-pid='' data-cid='' value="{{$c1['catid']}}" >{{$c1['catname']}}</option>
           {{foreach from=$c1['child'] item=c2}}
		  <option data-pid='{{$c1['catid']}}' data-cid='' value="{{$c2['catid']}}" >->{{$c2['catname']}}</option>
             {{foreach from=$c2['child'] item=c3}}
		    <option data-pid='{{$c1['catid']}}' data-cid='{{$c2['catid']}}' value="{{$c3['catid']}}" >--->{{$c3['catname']}}</option>
	      {{/foreach}}
           {{/foreach}}
	  {{/foreach}}
	  </select>
  </div>
  
  <div>
  	<input type="button" value="开始生成" onclick="javascript: do_make();" />
  </div>
  <hr />
</div>

<div id="output"></div>

<script>
var make = function(pid, cid, catid, title, fn){
	
  $('#output').prepend('<div>开始生成['+title+'] <span id="no_'+catid+'">0</span>/<span id="max_'+catid+'">1</span> <span style="color:red;" id="st_'+catid+'">makeing....</span></div>');
	$.getJSON('../index.php/category.index?pid=' + pid + '&cid=' + cid + '&catid=' + catid + '&do=makestatic', function(data) {
    if (data.status == 1) {
   	  $('#no_' + data.catid).html(1);
      if (typeof(data.max_page) != 'undefined') {
    	  $('#max_' + data.catid).html(data.max_page);
    	  var m = data.max_page - 1;
        for (var page = 2; page <= data.max_page; page++) {
        	$.getJSON('../index.php/category.index?pid=' + pid + '&cid=' + cid + '&catid=' + catid + '&do=makestatic&page=' + page, function(data) {
        		m--;
       			$('#no_' + data.catid).html(data.max_page-m);
        		if(m<=0) {
        			$('#st_' + data.catid).html('finish.');
        			fn();
        		}
          });
        }
      } else {
				$('#st_' + data.catid).html('finish.');
				fn();
      }
    } else {
			$('#st_' + data.catid).html('faild.');
    	fn();
    }
  });
};
var all_index = 1;
var all_make = function(){
	var p = $($('select[name=catid] option')[all_index]);
	make(p.attr('data-pid'), p.attr('data-cid'), p.attr('value'), p.html(), function(){
		all_index++;
		all_make();
	});
};
var do_make = function(){
	var sel_id = $('select[name=catid]').val();
	if(sel_id == 0) {
		all_make();
	} else {
		var p = $('select[name=catid]').find('option[value='+ sel_id +']');
		make(p.attr('data-pid'), p.attr('data-cid'), sel_id, p.html(), function(){
			
		});
	}
};
</script>

</body>
</html>